package com.cheyun.netsalev3.view.dm

import android.util.Log
import android.view.inputmethod.EditorInfo
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.databinding.ActivityDmSearchBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.viewmodel.DmSearchActivityViewModel
import com.google.android.material.tabs.TabLayout

class DmSearchActivity : BaseActivity<ActivityDmSearchBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_dm_search
    }

    override fun initViewModel(): ViewModel? {
        var factory = InjectorUtils.getDmSearchActivityViewModelFactory(this)
        return ViewModelProviders.of(this, factory).get(DmSearchActivityViewModel::class.java)
    }

    override fun initData() {
        binding.myEdit.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
                binding.viewModel?.search(v)
                true
            } else
                false
        }

        binding.viewModel?.searchlist?.observe(this, Observer { list ->
            adapter.submitList(list)
        })

        binding.tabLayout.addOnTabSelectedListener(object: TabLayout.BaseOnTabSelectedListener<TabLayout.Tab>{
            override fun onTabSelected(p0: TabLayout.Tab?) {
                Log.i("aa",p0?.position.toString());
//                binding.viewModel?.planType?.set(p0?.position)
                binding.viewModel?.status?.set(p0?.position)
            }
            override fun onTabReselected(p0: TabLayout.Tab?) {
            }
            override fun onTabUnselected(p0: TabLayout.Tab?) {

            }
        })
    }

    private lateinit var adapter: MyAdapter

    override fun initView() {
        adapter = MyAdapter(R.layout.dm_search_item, binding.viewModel)
        binding.recyclerView.adapter = adapter
    }
}
